import {defineStore} from "pinia";
import {ElMessage} from "element-plus";
import {addOrders, delOrders, findAllOrders, getOrders, updateOrders} from "@/api/orders";

export const OrdersStore = defineStore('orders', {
    state:()=>{
        return {
            // 列表
            ordersList: [],
            // 详细信息
            ordersInfo: {}
        }
    },
    actions:{
        addOrders(){
            return new Promise((resolve, reject)=>{
                addOrders(this.ordersInfo).then(res => {
                    resolve(res.result)
                    this.ordersInfo = {}
                }).catch(err => {
                    reject()
                })
            })
        },
        async getOrdersList(query) {
            return new Promise((resolve, reject)=>{
                findAllOrders(query).then(res => {
                    this.ordersList = res.result.list
                    resolve(res.result)
                }).catch(err => {
                    reject()
                })
            })
        },
        async getOrdersInfo(id) {
            const {result} = await getOrders(id)
            this.ordersInfo = result
            this.ordersInfo.orders_price = parseFloat(this.ordersInfo.orders_price)
        },
        updateOrders(id,data) {
            return new Promise(async (resolve, reject) => {
                updateOrders(id,data).then(res => {
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },
        async deleteOrders(id) {
            const {code} = await delOrders(id)
        },
    }
})